package com.hudongwx.food.activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import com.hudongwx.food.R;
import com.hudongwx.food.utils.MyJsonObject;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import cn.sharesdk.onekeyshare.OnekeyShare;
public class XiangqingActivity extends AppCompatActivity {
    MyRecycleAdapterTuiJian adapter;
    JSONObject jobj;
    Intent intent;
    String type;
    boolean flg1 = false;
    boolean flg2 = false;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiangqing);
        recyclerView = (RecyclerView) findViewById(R.id.xiangqing_recyclerView);
        parseShan();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new MyRecycleAdapterTuiJian(jobj);
        recyclerView.setAdapter(adapter);
    }

    //recycleAdapter
    class MyRecycleAdapterTuiJian extends RecyclerView.Adapter<MyRecycleViewHolderTuiJian> {
        JSONObject jobj;

        public MyRecycleAdapterTuiJian(JSONObject jobj) {
            this.jobj = jobj;
        }

        public void updateC2(JSONObject jobj) {
            this.jobj = jobj;
            notifyDataSetChanged();
        }

        @Override
        public MyRecycleViewHolderTuiJian onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.xiangqing_list, null);
            return new MyRecycleViewHolderTuiJian(view);
        }


        @Override
        public void onBindViewHolder(MyRecycleViewHolderTuiJian holder, int position) {
            if (null == jobj) {
                recyclerView.setVisibility(View.GONE);
            } else {
                try {
                    x.image().bind(holder.imageView1, jobj.getString("ImgUrl"));
                    holder.textView8.setText(jobj.getString("ProductName"));
                    holder.textView1.setText(jobj.getString("ShortName"));
                    holder.textView2.setText(jobj.getString("UnitPrice"));
                    holder.textView4.setText(jobj.getString("Spec"));
                    holder.textView7.setText(jobj.getString("ProductAttributes"));
                    holder.webView.loadDataWithBaseURL("about:blank", jobj.getString("AppDescribe"), "text/html", "utf-8", null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            if (null == jobj) return 0;
            return 1;
        }
    }

    class MyRecycleViewHolderTuiJian extends RecyclerView.ViewHolder {
        TextView textView1, textView2, textView3, textView4, textView5, textView6, textView7, textView8;
        WebView webView;
        ImageView imageView1, i2,i3;

        public MyRecycleViewHolderTuiJian(View itemView) {
            super(itemView);
            textView1 = (TextView) itemView.findViewById(R.id.detail_tv_1);
            textView2 = (TextView) itemView.findViewById(R.id.detail_tv_2);
            textView3 = (TextView) itemView.findViewById(R.id.detail_tv_3);
            textView4 = (TextView) itemView.findViewById(R.id.detail_tv_4);
            textView5 = (TextView) itemView.findViewById(R.id.detail_tv_5);
            textView6 = (TextView) itemView.findViewById(R.id.detail_tv_6);
            textView7 = (TextView) itemView.findViewById(R.id.detail_tv_7);
            textView8 = (TextView) itemView.findViewById(R.id.detail_tv_8);
            webView = (WebView) itemView.findViewById(R.id.detail_web);
            imageView1 = (ImageView) itemView.findViewById(R.id.detail_iv_1);
            i2 = (ImageView) itemView.findViewById(R.id.xiangqing_fenxing);
            i3 = (ImageView) itemView.findViewById(R.id.food_back);
            i3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            i2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
            textView4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.isSelected() && !flg1) {
                        v.setSelected(false);
                        textView5.setSelected(true);
                        textView4.setTextColor(Color.BLACK);
                        textView5.setTextColor(Color.WHITE);
                        flg2 = false;
                    } else {
                        flg1 = true;
                        v.setSelected(true);
                        textView5.setSelected(false);
                        textView4.setTextColor(Color.WHITE);
                        textView5.setTextColor(Color.BLACK);

                    }
                }
            });
            textView5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.isSelected() && !flg2) {
                        flg1 = false;
                        v.setSelected(false);
                        textView4.setSelected(true);
                        textView5.setTextColor(Color.BLACK);
                        textView4.setTextColor(Color.WHITE);

                    } else {
                        flg2 = true;
                        v.setSelected(true);
                        textView4.setSelected(false);
                        textView5.setTextColor(Color.WHITE);
                        textView4.setTextColor(Color.BLACK);

                    }
                }
            });

        }
    }

    //解析靓贴
    public void parseShan() {
        intent = getIntent();
        type = intent.getStringExtra("type");
        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Product?productId=" + type);
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONObject data1 = result.getJSONObject("Data");
                    adapter.updateC2(data1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("ShareSDK");
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }
}
